#include "Netvars.h"

int NetvarManager::CrawlTableForOffset(RecvTable* pTable, const char* name)
{
	int addOffset = 0;
	for(int i = 0; i < pTable->GetNumProps(); i++)
	{
		RecvProp *pProp = pTable->xGetProp(i);

		JUNK_CODE;
		if(pProp->GetDataTable() && pProp->GetDataTable()->GetNumProps() > 0)
		{
			int iOffset = CrawlTableForOffset(pProp->GetDataTable(), name);
			if(iOffset)
			{
				JUNK_CODE;
				addOffset += pProp->GetOffset() + iOffset;
			}
		}

		std::string propName(pProp->GetName());
		JUNK_CODE;
		if(propName.find(/*[0]*/XorStr(3, 0x67, 0x3CD63800).c_str()) != propName.npos)
			propName.replace( propName.find(/*[0]*/XorStr(3, 0x67, 0x3CD63800).c_str()), 3, "");

		JUNK_CODE;
		if(_stricmp(name, propName.c_str()))
			continue;

		return pProp->GetOffset() + addOffset;
	}
	return addOffset;
}

int NetvarManager::GetNetvarOffset(const char *table, const char *name)
{
	ClientClass *pClass = g_pInterfaces.g_pClient->GetAllClasses(); // TODO get interface!
	for(; pClass; pClass = pClass->m_pNext)
	{
		RecvTable *pTable = pClass->m_pRecvTable;
		JUNK_CODE;
		if(pTable->GetNumProps() <= 1)
			continue;

		if(_stricmp(table, pTable->GetName()))
			continue;

		int iResult = CrawlTableForOffset(pTable, name);

		return iResult;
	}
	return NULL;
}

void NetvarManager::PullOffsets()
{
	Offsets::m_iTeamNum	     = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_iTeamNum*/XorStr(10, 0xAC, 0xC174C37D, 0xCD46CB6B, 0xD14E0000).c_str());
	Offsets::m_lifeState	 = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_lifeState*/XorStr(11, 0x25, 0x48FB4FCB, 0x47C54CEA, 0x7CE87E00).c_str());
	JUNK_CODE;
	Offsets::m_iHealth		 = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_iHealth*/XorStr(9, 0x31, 0x5CEF46E6, 0x48CD47DE, 0x41000000).c_str());
	Offsets::m_fFlags		 = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_fFlags*/XorStr(8, 0xCA, 0xA716AE01, 0xAA24A330).c_str());
	Offsets::m_ArmorValue	 = GetNetvarOffset(/*DT_CSPlayer*/XorStr(11, 0x93, 0xD746CE53, 0xDC5EE16D, 0xF26FFB00).c_str(),	/*m_ArmorValue*/XorStr(12, 0x20, 0x4DC05FEF, 0x71F468CF, 0x79FB63F0).c_str());
	JUNK_CODE;
	Offsets::m_hActiveWeapon = GetNetvarOffset(/*DT_BaseCombatCharacter*/XorStr(22, 0x8B, 0xCF5ED64A, 0xE675E047, 0xEC6FE361, 0x0BBD159D, 0x099B1A8C, 0x12840000).c_str(), /*m_hActiveWeapon*/XorStr(15, 0x99, 0xF447FF57, 0xF660FA64, 0xF447EA6F, 0xFD63E500).c_str());
	Offsets::m_vecVelocity   = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_vecVelocity*/XorStr(13, 0xA1, 0xCC7FE97B, 0xFE4AFE76, 0xF67BFE62, 0xEC000000).c_str());
	Offsets::m_vecOrigin	 = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_vecOrigin*/XorStr(11, 0x30, 0x5DF058C8, 0x4FE458C0, 0x4FCE4800).c_str());
	Offsets::m_vecPunchAngle = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_vecPunchAngle*/XorStr(15, 0x23, 0x4EFD57C5, 0x7CCE68F2, 0x78F258F6, 0x70FA7000).c_str());
	JUNK_CODE;
	Offsets::m_iFOV			 = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_iFOV*/XorStr(6, 0xCD, 0xA013A20C, 0x861E0000).c_str());
	Offsets::m_vecViewOffset = GetNetvarOffset(/*DT_BasePlayer*/XorStr(13, 0xA1, 0xE574C05C, 0xFC6FFE4A, 0xF579EE73, 0xE7000000).c_str(), /*m_vecViewOffset*/XorStr(15, 0x75, 0x18AB0597, 0x12A6068B, 0x1AA30D8C, 0x1A8D1300).c_str());
	Offsets::m_iClip1		 = GetNetvarOffset(/*DT_BaseCombatWeapon*/XorStr(19, 0x4E, 0x0A991389, 0x2BBA2D84, 0x29A826A2, 0x369625DE, 0x4ED25200).c_str(), /*m_iClip1*/XorStr(8, 0xCE, 0xA312A508, 0xA620B876).c_str());
	JUNK_CODE;
	Offsets::m_hOwner		 = GetNetvarOffset(/*DT_BaseCombatWeapon*/XorStr(19, 0x6B, 0x2FBE36AA, 0x069500A7, 0x0C8F0381, 0x2B8938BD, 0x2BB53700).c_str(), /*m_hOwner*/XorStr(8, 0x44, 0x299C2A8E, 0x37D15BCF).c_str());
}